package com.groupbuying.currency.producer.utils.currencyOperUtils;

import java.math.BigDecimal;

/**
 * @auter miaozhenfei
 * @date 2019/7/26 10:05
 */
public class DecimalCalculationUtils {
    //增加
    public static double add(double augend, double addend) {
        BigDecimal bgAugend = new BigDecimal(augend + "");
        BigDecimal bgAddend = new BigDecimal(addend + "");
        return bgAugend.add(bgAddend).doubleValue();
    }

    //减少
    public static double subtract(double minuend, double meiosis) {
        BigDecimal bgMinuend = new BigDecimal(minuend + "");
        BigDecimal bgMeiosis = new BigDecimal(meiosis + "");
        return bgMinuend.subtract(bgMeiosis).doubleValue();
    }

    //乘以
    public static double multiply(double faciend, double multiplier) {
        BigDecimal bgFaciend = new BigDecimal(faciend + "");
        BigDecimal bgMultiplier = new BigDecimal(multiplier + "");
        return bgFaciend.multiply(bgMultiplier).doubleValue();
    }

    //比较 >0,<0,=0
    public static int compareBalance(Double newBalance, Double oldBalance) {

        BigDecimal before1 = new BigDecimal(oldBalance.toString());
        BigDecimal after1 = new BigDecimal(newBalance.toString());
        int retval = before1.compareTo(after1);
        return retval;
    }

    public static double multiplyScale2RoundHalfUp(double faciend, double multiplier) {
        return multiply(faciend, multiplier, 2, 4);
    }

    public static double multiply(double faciend, double multiplier, int scale, int roundingMode) {
        BigDecimal bgFaciend = new BigDecimal(faciend + "");
        BigDecimal bgMultiplier = new BigDecimal(multiplier + "");
        return divide(bgFaciend.multiply(bgMultiplier).doubleValue(), 1.0D, scale, roundingMode);
    }

    public static double divide(double dividend, double divisor) {
        return divide(dividend, divisor, 2, 4);
    }

    public static double divide(double dividend, double divisor, int scale, int roundingMode) {
        if (divisor == 0.0D) {
            return 0.0D;
        } else {
            BigDecimal bgDividend = new BigDecimal(dividend + "");
            BigDecimal bgDivisor = new BigDecimal(divisor + "");
            return bgDividend.divide(bgDivisor, scale, roundingMode).doubleValue();
        }
    }

    public static double string2double(String number) {
        BigDecimal bg = new BigDecimal(number);
        return bg.doubleValue();
    }

    public static boolean comparisonSizeForBig(double before, double after) {
        BigDecimal before1 = new BigDecimal(before);
        BigDecimal after1 = new BigDecimal(after);
        return before1.compareTo(after1) > 1;
    }

    public static boolean comparisonEqual(double before, double after) {
        BigDecimal before1 = new BigDecimal(before);
        BigDecimal after1 = new BigDecimal(after);
        return before1.compareTo(after1) == 0;
    }

    public static boolean comparisonSizeForSmall(double before, double after) {
        BigDecimal before1 = new BigDecimal(before);
        BigDecimal after1 = new BigDecimal(after);
        return before1.compareTo(after1) < 0;
    }

    private DecimalCalculationUtils() {
    }
}
